<template>
	<div class="login container">
		<Header>
			<span>
				找回密码
			</span>
		</Header>
		<section>
			<div class='login-tel'>
				<input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
			</div>
			<div class='login-code'>
				<input type="text" v-model="userCode" placeholder="请输入短信验证码" pattern="[0-9]*">
				<button :disabled="disabled" @click="sendCode">{{codeMsg}}</button>
			</div>
			<div class='login-btn' @click='next'>下 一 步</div>
			<!-- <div class='tab'>
				<span @click='goUserLogin'>密码登录</span>
				<span @click='goRegister'>快速注册</span>
			</div> -->
		</section>
		<Tabbar></Tabbar>
	</div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue'
import Header from '@/views/login/Header.vue'
import { Toast } from 'mint-ui';
import http from '@/common/api/request.js'
export default{
	data(){
		return {
			userTel:'17724528177',//用户输入的手机号 17724528177
			userCode:'',
			disabled:false,
			//验证规则
			rules:{
				//手机号验证
				userTel:{
					rule:/^1[23456789]\d{9}$/,
					msg:'手机号不能为空，并且是11位数字'
				}
			},
			codeNum:60,
			codeMsg:'获取短信验证码',
			code:''
		}
	},
	components:{
		Header,
		Tabbar
	},
	
	methods:{
		//用户登录
		goUserLogin(){
			this.$router.push('/userLogin');
		},
		//获取短信验证码按钮
		sendCode(){
			if( !this.validate('userTel') ) return;
			console.log(this.userTel);
			//请求短信验证码接口
			http.$axios({
				url:'/api/code',
				method:'POST',
				data:{
					phone:this.userTel
				}
			}).then(res=>{
				console.log(res.success);
				if( res.success ){
					console.log(res);
					
					this.code = res.data;
				}
			})
			
			console.log(this.code);
			//禁用按钮
			this.disabled = true;
			console.log("按钮已禁用");
			//倒计时
			let timer = setInterval(()=>{
				--this.codeNum;
				this.codeMsg = `重新发送 ${this.codeNum}`;
			},1000)
			//判断什么时候停止定时器
			setTimeout(()=>{
				clearInterval( timer );
				this.codeNum = 60;
				this.disabled = false;
				this.codeMsg = '获取短信验证码';
			},60000)
		},
		//验证信息提示
		validate( key ){
			let bool = true;
			if( !this.rules[key].rule.test( this[key])  ){
				//提示信息
				Toast(this.rules[key].msg);
				bool = false;
				return false;
			}
			return bool;
		},
		//点击下一步
		next(){
			console.log("下一步");
			console.log(this.code);
			console.log(this.code == this.userCode);
			if(this.userCode == ""){
				Toast('验证码为空,请输入验证码');
				return ;
			}else{
				//验证码不正确
				if(this.code  !=  this.userCode ){
					Toast('验证码不正确');
					return ;
				}
				//验证码正确
				if( this.code  ==  this.userCode ){
					//证明用户输入的短信验证码是正确的
					this.$router.push({
						name: 'RecoveryBtn'
					})
					//发送请求
					http.$axios({
						url:'/api/selectUser',
						method:'POST',
						data:{
							phone:this.userTel
						}
					}).then(res=>{
						if( !res.success ){
							Toast(res.msg);
							return;
						} 
						console.log( res );
						this.$router.push({
							name: 'RecoveryBtn',
							query:{
								phone:this.userTel
							}
						})
					})
					
				}
			}
			
		},
		
	}
}
</script>

<style scoped lang="scss">
	section{
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color:#f5f5f5;
		div{
			margin:15px 0;
			width: 335px;
			height: 44px;
		}
		input{
			box-sizing: border-box;
			padding:0 10px;
			line-height: 44px;
			background-color: #FFFFFF;
			border:1px solid #ccc;
			border-radius: 6px;
		}
		.login-tel{
			margin-top:30px;
			input{
				width: 335px;
			}
		}
		.login-code{
			display: flex;
			input{
				flex:1;
			}
			button{
				margin-left: 10px;
				padding:0 20px;
				line-height: 44px;
				color:#fff;
				background-color: #b0352f;
				border:0;
				border-radius: 6px;
			}
		}
		.login-btn{
			line-height: 44px;
			font-size: 25px;
			color:#fff;
			text-align: center;
			background-color: #b0352f;
			border-radius: 6px;
		}
		.tab{
			display: flex;
			justify-content: space-between;
			font-size: 14px;
		}
	}
</style>
